package poker;
import java.io.*;
public class Table {
	Deck d;
    Player[] players;
    Human[] humans;
    Bot[] bots;
    int size=0;
	public Table(int numofH,int numofB) {
		size=numofH+numofB;
		players=new Player[size];		
	}
	
	public void giveCards(Card[] Hand){
		int index;
		for(int k=0;k<size;k++){
			for(index=0;index<=4;index++){
				players[k].Hand[index]=d.drawCard();
			}
		}
	}
	public void changeCards(Card[] Hand,int index){
		for(int k=0;k<size;k++){
			players[k].Hand[index]=d.drawCard();
		}
	}
	/*public Card[] switchCards(Card[] Hand,int index,int minimal){
		Card temp;
		temp=Hand[index];
		Hand[index]=Hand[minimal];
		Hand[minimal]=temp;
		return Hand;
	}
	public Card[] sortHand(Card[] Hand){
		int index,x,minimal;
		for(x=0;x<4;x++){
			minimal=x;
			for(index=x;index<5;index++){
				if(Hand[index].getRank()<Hand[minimal].getRank())
					minimal=index;
				Hand=switchCards(Hand,x,minimal);
			}
		}
		return Hand;
	}*/
	public void startThePoker(int numofH,int numofB){
		Deck d=new Deck();
		int i;
		d.shuffle();
		for(i=0;i<=numofH;i++){
				humans[i]=new Human(this);
		}
		bots=new Bot[numofB];
		for(i=0;i<numofH;i++){
			humans[i].WhatToDo();
		}
		for(i=0;i<numofH;i++){
			bots[i].ChangeMyCards();
		}
		System.out.println(winner(players));
		
	}
	//===============WIN===============================
	public Player winner(Player[] players){
		bestConfig(players);
		return players[0];
	}
	public void bestConfig(Player[] players){
		int value=players.length;
		int i,j;
		for(i=0;i<value;i++){
			for(j=1;j<value-i;j++){
				if(getConfigWeight(players[j-1].Hand)<getConfigWeight(players[j].Hand)){
					Player temp=players[j-1];
					players[j-1]=players[j];
					players[j]=temp;
				}
				if(getConfigWeight(players[j-1].Hand)==getConfigWeight(players[j].Hand)){
					if(getConfigWeight(players[j-1].Hand)==8){				//poker
						if(players[j-1].Hand[4].getRank()<players[j-1].Hand[4].getRank()){
							Player temp=players[j-1];
							players[j-1]=players[j];
							players[j]=temp;
						}
						if(players[j-1].Hand[4]==players[j].Hand[4]){
						//	Player remis[j]=players[j-1];
						//	Player remis[j+1]=players[j];
						}
					}
					if(getConfigWeight(players[j-1].Hand)==7){		//four of a kind
						if(players[j-1].Hand[2].getRank()<players[j].Hand[2].getRank()){
							Player temp=players[j-1];
							players[j-1]=players[j];
							players[j]=temp;
						}
					}
					if(getConfigWeight(players[j-1].Hand)==6){		//full
						if(players[j-1].Hand[2].getRank()<players[j].Hand[2].getRank()){
							Player temp=players[j-1];
							players[j-1]=players[j];
							players[j]=temp;
						}
					}
					if(getConfigWeight(players[j-1].Hand)==5){		//colour
						if(players[j-1].Hand[4].getRank()<players[j].Hand[4].getRank()){
							Player temp=players[j-1];
							players[j-1]=players[j];
							players[j]=temp;
						}
						if(players[j-1].Hand[4].getRank()==players[j-1].Hand[4].getRank()){
							//remis
						}
					}
					if(getConfigWeight(players[j-1].Hand)==4){		//strit
						if(players[j-1].Hand[4].getRank()<players[j].Hand[4].getRank()){
							Player temp=players[j-1];
							players[j-1]=players[j];
							players[j]=temp;
						}
						if(players[j-1].Hand[4].getRank()==players[j].Hand[4].getRank()){
							//remis
						}
					}
					if(getConfigWeight(players[j-1].Hand)==3){		//triple
						if(players[j-1].Hand[2].getRank()<players[j].Hand[2].getRank()){	
							Player temp=players[j-1];
							players[j-1]=players[j];
							players[j]=temp;
						}
					}
					if(getConfigWeight(players[j-1].Hand)==2){		//double
						int x,k;
						int highest1=0;
						int highest2=0;
						for(x=0;x<2;x++){
							if(players[j].Hand[x].getRank()==players[j].Hand[x+1].getRank()){
								for(k=x;k<4;k++){
									if((players[j].Hand[k].getRank()==players[j].Hand[k+1].getRank())&&(players[j].Hand[x].getRank()!=players[j].Hand[k].getRank())){
										if(players[j].Hand[k].getRank()>players[j].Hand[x].getRank())	
											highest1=players[j].Hand[k].getRank();
										else highest1=players[j].Hand[k].getRank();
									}
								}
							}
						}
						for(x=0;x<2;x++){
							if(players[j-1].Hand[x].getRank()==players[j-1].Hand[x+1].getRank()){
								for(k=x;k<4;k++){
									if((players[j-1].Hand[k].getRank()==players[j-1].Hand[k+1].getRank())&&(players[j-1].Hand[x].getRank()!=players[j-1].Hand[k].getRank())){
										if(players[j-1].Hand[k].getRank()>players[j-1].Hand[x].getRank())	
											highest2=players[j-1].Hand[k].getRank();
										else highest2=players[j-1].Hand[k].getRank();
										
									}
								}
							}
						}
						if(highest2>highest1){
							Player temp=players[j-1];
							players[j-1]=players[j];
							players[j]=temp;
						}
						if(highest2==highest1){
							int kicker1=0;
							int kicker2=0;
							for(x=0;x<4;x++){
								for(k=4;k>0;k--){
									if((players[j-1].Hand[k].getRank()!=players[j-1].Hand[k-1].getRank())&&(players[j-1].Hand[k].getRank()!=players[j-1].Hand[x].getRank())){
										kicker2=players[j-1].Hand[k].getRank();
									}
								}
							}
							for(x=0;x<4;x++){
								for(k=4;k>0;k--){
									if((players[j].Hand[k].getRank()!=players[j].Hand[k-1].getRank())&&(players[j].Hand[k].getRank()!=players[j].Hand[x].getRank())){
										kicker1=players[j].Hand[k].getRank();
									}
								}
							}
							if(kicker2>kicker1){
								Player temp=players[j-1];
								players[j-1]=players[j];
								players[j]=temp;
							}
						}
					}
					if(getConfigWeight(players[j-1].Hand)==1){				
						int highest1=0;
						int highest2=0;
						int x;
						for (x=0;x<4;x++){
							if(players[j].Hand[x].getRank()==players[j].Hand[x+1].getRank()){
								highest1=players[j].Hand[i].getRank();
							}
							if(players[j-1].Hand[x].getRank()==players[j-1].Hand[x+1].getRank()){
								highest2=players[j-1].Hand[i].getRank();
							}
						}
						if(highest2>highest1){
							Player temp=players[j-1];
							players[j-1]=players[j];
							players[j]=temp;
						}
					}
				}
			}
		}
	}
	
	public int getConfigWeight(Card[] Hand){
		int configweight=0;
		if(pair(Hand)) configweight=1;
		if(doublePair(Hand)) configweight=2;
		if(triple(Hand)) configweight=3;
		if(strit(Hand)) configweight=4;
		if(colour(Hand)) configweight=5;
		if(full(Hand)) configweight=6;
		if(four(Hand)) configweight=7;
		if(poooker(Hand)) configweight=8;
		return configweight;
	}
//===========================CONFIGS================================
	int cardvalue=0;
	public boolean pair(Card[] Hand){
		int i;
		for (i=0;i<4;i++){
				if(Hand[i].getRank()==Hand[i+1].getRank()){
					cardvalue=Hand[i].getRank();
					return true;
				}
					
		}
		return false;
	}
		
	public boolean doublePair(Card[] Hand){
		int i,k;
		for(i=0;i<2;i++){
			if(Hand[i].getRank()==Hand[i+1].getRank()){
				for(k=i;k<4;k++){
					if((Hand[k].getRank()==Hand[k+1].getRank())&&(Hand[i].getRank()!=Hand[k].getRank())){
						if(Hand[k].getRank()>Hand[i].getRank())
							cardvalue=Hand[k].getRank();
						else cardvalue=Hand[i].getRank();
						return true;
					}
				}
			}
		}
		return false;
	}
	
	public boolean triple(Card[] Hand){
		int i;
		for(i=0;i<3;i++){
			if((Hand[i].getRank()==Hand[i+1].getRank())&&(Hand[i].getRank()==Hand[i+2].getRank())){
				cardvalue=Hand[i].getRank();
				return true;
			}
		}
		return false;
	}
	
	public boolean strit(Card[] Hand){
		int crd=Hand[0].getRank();
		int i;
		for(i=0;i<5;i++){
			if(crd+i !=Hand[i].getRank())
				return false;
		}
		cardvalue=Hand[4].getRank();
		return true;
	}
	
	public boolean colour(Card[] Hand){
		int crd=Hand[0].getSuit();
		int i;
		for (i=0;i<5;i++){
			if(crd!=Hand[i].getSuit())
				return false;
		}
		cardvalue=Hand[4].getRank();
		return true;
	}
	
	public boolean full(Card[] Hand){
		if((pair(Hand))&&(triple(Hand))){
			cardvalue=Hand[2].getRank();
			return true;
		}
		return false;
	}
	
	public boolean four(Card[] Hand){
		int i;
		for(i=0;i<2;i++){
			if((Hand[i].getRank()==Hand[i+1].getRank())&&(Hand[i].getRank()==Hand[i+2].getRank())&&(Hand[i].getRank()==Hand[i+3].getRank())){
				cardvalue=Hand[i].getRank();
				return true;
			}
		}
		return false;
	}
	public boolean poooker(Card[] Hand){
		if(strit(Hand)&&colour(Hand)){
			cardvalue=Hand[4].getRank();
			return true;
		}
		return false;
	}
}
